package ym.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.Role;
import ym.service.RoleService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台角色接口
 */
@RestController
@RequestMapping("/back/role")
@CrossOrigin
public class RoleController {
    @DubboReference
    private RoleService roleService;

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/sys')")
    public BaseResult add(Role role) {
        roleService.add(role);
        return BaseResult.ok();
    }


    /**
     * 修改角色
     *
     * @param role
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(Role role) {
        roleService.update(role);
        return BaseResult.ok();
    }


    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer roleId) {
        roleService.delete(roleId);
        return BaseResult.ok();
    }


    /**
     * 根据id查询角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Role> findById(Integer roleId) {
        Role role = roleService.findById(roleId);
        return BaseResult.ok(role);
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult<List<Role>> findAll() {
        List<Role> all = roleService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 分页查询角色
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/sys')")
    public BaseResult<Page<Role>> search(int page, int size) {
        Page<Role> page1 = roleService.search(page, size);
        return BaseResult.ok(page1);
    }


    /**
     * 修改角色的权限
     *
     * @param roleId
     * @param permissionIds
     * @return
     */
    @PostMapping("/updatePermission")
    public BaseResult updatePermission(Integer roleId, Integer[] permissionIds) {
        roleService.updatePermission(roleId, permissionIds);
        return BaseResult.ok();
    }
}
